<!doctype html>
<html>

<head>
	<title></title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
	<link rel="stylesheet" href="../res/css/weui.css" />
	<link rel="stylesheet" href="../res/css/weuix.css" />
	<link rel="stylesheet" href="../res/css/common.css" />
	<script src="../res/js/zepto.min.js"></script>
	<script src="../res/js/zepto.weui.js"></script>
	<script src="../res/js/common.js?v="></script>
	<style type="text/css">
		.weui-cell {
			display: block;
			position: relative;
			padding: 10px 15px;
			top: -60px;
		}

		.weui-btn_primary {
			background-color: #1189ff;
		}

		.weui-header .weui-header-title {
			color: #FFFFFF;
		}

		.weui-btn_primary:not(.weui-btn_disabled):active {
			color: rgba(255, 255, 255, 0.6);
			background-color: #1189ff;
		}

		.weui-cell__bd {
			height: 50px;
			line-height: 50px;
			border-bottom: 1px solid #E0E0E0;
			position: relative;
		}

		.placeholder {
			background-image: url('https://yixiang-hospital.oss-cn-shenzhen.aliyuncs.com/mini/minImg/img_bg.png');
			background-size: 100% 100%;
			background-repeat: no-repeat;
			display: flex;
		}

		.placeholder img:first-child {
			margin: 0 auto;
			width: 40%;
			height: 30%;
			margin-top: 60px;
		}

		.placeholder img:last-child {
			margin: 0 auto;
			width: 50%;
			height: 75%;
			margin-top: 25px;
		}

		.weui-form-checkbox[type="radio"]:checked+label .weui-icon-radio {
			background-color: #FCA92B;
			border: 1px solid #1189ff;
		}

		.weui-icon-radio {
			height: 12px;
			width: 12px;
			vertical-align: initial;
			border: 1px solid #1189ff;
		}

		.weui-form-li {
			padding-left: 5px;
			margin: 5px 0;
		}

		.left_img {
			width: 20px;
			height: 18px;
			display: inline-block;
			vertical-align: middle;
			z-index: 1;
		}

		.weui-input {
			padding-left: 10px;
			width: 85%;
		}

		/* .weui-footer_fixed-bottom {
			position: absolute;
		} */

		/* #v_container {
			margin-top: 3px;
			width: 120px;
			height: 44px;
			line-height: 42px;
			text-align: center;
			border-radius: 25px;
			background-color: #1189ff;
			color: #ffffff;
			position: absolute;
			right: 0;
			top: 0;
		} */

		.notClickn {
			pointer-events: none;
			opacity: 0.5;
		}

		.disappear {
			display: none;
		}

		.selfChoose {
			display: none;
			justify-content: space-around;
			margin-bottom: 20rpx !important;
		}
		#idCardInput{
			display: none;
		}

		.position-rela{
			position: relative;
		}

		.flex-row{
			display: flex;
			align-items: center;
		}
		.weui-btn.get-code{
			width: unset;
			flex-grow: 0;
			font-size: 16px;
			line-height: 36px;
			margin-right: 0;
			color: #fff;
		}
		.cell-agree{
			margin-top: 10px;
			font-size: 14px;
		}
		#agreementTitle{
			color: #1189ff;
		}

		.togglePsw{
			width: 21px;
			height: 16px;
			display: none;
			position: absolute;
			right: 10px;
			top: 50%;
			transform: translateY(-50%);
		}
		.togglePsw.eye-open{
			height: 27px;
		}
	</style>
</head>

<body ontouchstart>
	<div class="fixed-top">
		<div class="weui-header weui-btn_primary">
			<div class="weui-header-left"><a class="icon icon-109 f-white" id="btnBack"></a></div>
			<div class="weui-header-title">用户登录</div>
			<div class="weui-header-right"></div>
		</div>
	</div>
	<div class="weui-cells weui-cells_form" style="margin-top:0px;">
		<div class="weui-flex">
			<div class="weui-flex__item">
				<div class="placeholder">
					<img src="https://yixiang-hospital.oss-cn-shenzhen.aliyuncs.com/mini/minImg/img_hello@3x.png" class="img-max">
					<img src="https://yixiang-hospital.oss-cn-shenzhen.aliyuncs.com/mini/minImg/img_doctor.png" class="img-max">
				</div>
			</div>
		</div>
		<div class="weui-cell">
			<div class="selfChoose">
				<div>
					<input class="weui-form-checkbox" name="workChoose" id="identityF1" value="1" type="radio"
						onchange="identityChange(0)">
					<label for="identityF1"><i class="weui-icon-radio"></i>
						<div class="weui-form-text">
							<p>手机号&nbsp;&nbsp;&nbsp;</p>
						</div>
					</label>
				</div>
				<div>
					<input class="weui-form-checkbox" name="workChoose" id="identityF2" value="2" type="radio"
						onchange="identityChange(1)">
					<label for="identityF2"><i class="weui-icon-radio"></i>
						<div class="weui-form-text">
							<p>身份证号&nbsp;&nbsp;&nbsp;</p>
						</div>
					</label>
				</div>
			</div>
			<div class="weui-cell__bd" id="txtPhone">
				<img src="../res/images/icon_user.png" class="left_img">
				<input class="weui-input" placeholder="请输入手机号码" class="weui_input" required id="txt_phone" type="tel"
					maxlength="11" oninput="check()">
			</div>
			<div class="weui-cell__bd" id="idCardInput">
				<img src="../res/images/icon_user.png" class="left_img">
				<input class="weui-input" placeholder="请输入登录身份证号码" class="weui_input" required id="txt_IDcard"
					type="text" maxlength="18" onblur="checkId()">
			</div>
			<div class="weui-cell__bd">
				<img src="../res/images/icon_password.png" class="left_img">
				<input class="weui-input" placeholder="请输入密码" class="weui_input" required id="txt_paw" 
					type="password">
				<img src="" alt="" class="togglePsw">
			</div>
			<div class="weui-cell__bd position-rela flex-row" id="SMS">
				<img src="../res/images/icon_password.png" class="left_img">
				&nbsp;
				<input class="weui-input" placeholder="请输入验证码" class="weui_input" required id="code_input" type="tel" style="width: 120px;" maxlength="4">
				<div id="v_container" class="weui-btn weui-btn_primary get-code" onclick="get_captcha()">获取验证码</div>
			</div>
			<div class="cell-agree" id="agreementBox">
				<input type="checkbox" name="read-agree" id="agree">
				<label for="agree" id="agreeLabel">查看并同意</label><span id="agreementTitle"></span>
			</div>
			<a href="javascript:;" class="weui-btn weui-btn_primary" style="    margin-top: 100px;color: #fff;"
				onclick="select()">登&nbsp;&nbsp;录</a>

			<div class="weui-flex" style="padding:5px 10px 0 10px; display: block;text-align: center;">
				<a href="javascript:;" onclick="doForget()">忘记密码</a>
				|
				<a href="javascript:;" onclick="doReg()">立即注册</a>
			</div>
		</div>
	</div>
	<div class="weui-mask" style="opacity: 1;"></div>
	<div class="weui-dialog" style="opacity: 1;">
		<div class="weui-dialog__title feeInfo" style="border-radius: 0px;">
			请选择登录角色
		</div>
		<div class="weui-dialog__bd feeInfoDate">
			<div class="weui-form-li">
				<input class="weui-form-checkbox" name="f1" id="f1" value="1" type="radio">
				<label for="f1"><i class="weui-icon-radio"></i>
					<div class="weui-form-text">
						<p>用户&nbsp;&nbsp;&nbsp;</p>
					</div>
				</label>
			</div>
			<div class="weui-form-li">
				<input class="weui-form-checkbox" name="f1" id="f2" value="2" type="radio">
				<label for="f2"> <i class="weui-icon-radio"></i>
					<div class="weui-form-text">
						<p>服务点</p>
					</div>
				</label>
			</div>
		</div>
		<div class="weui-dialog__ft feeInfoSelect">
			<a href="javascript:;" class="weui-dialog__btn default" onclick="disappear()">取消</a>
			<a href="javascript:;" class="weui-dialog__btn primary" onclick="Gologin()">确定</a>
		</div>
	</div>
	<div class="weui-footer weui-footer_fixed-bottom">
		<p class="weui-footer__text">Copyright &copy; 2020 <a href="http://www.yixianmedical.com/"
				style="color:Gray;">一线医疗</a></p>
	</div>
	<script src="../res/js/jsencrypt.js"></script>
	<script>
		var strUserType, roles = 0,
			role = 0;
		var strUserName, strPassWord, wxcode, userIdCard = '';
		var SMScode = 0;
		var countdown = 60;
		var userJson = {};
		var isIdLogin = 1; //是否是身份证登录 1否，2是
		var userNotFound = false;

		var agreementTitle = ''	//用户协议标题

		$(function () {
			getCode()
			var oHeight = $(document).height(); //浏览器当前的高度

			//1.9.7
			//HIS配置判断，在common.js文件中配置
			// if (!isHis) {
				$(".selfChoose").css({
					"display": "none"
				})
				$("#idCardInput").css({
					"display": "none"
				})
			// } else {
			// 	$("#identityF1").attr('checked', true);
			// 	$("#idCardInput").css({
			// 		"display": "none"
			// 	})
			// }

			$('.weui-cells').on('focus', 'input', function () {
				$('.weui-footer').css({
					"position": "static",
					"display": "none"
				});
			})
			$('.weui-cells').on('blur', 'input', function () {
				$('.weui-footer').css({
					"position": "fixed",
					"display": "block"
				});
			})

			let oldLogMsg = localStorage.getItem('oldLogMsg')
			if(oldLogMsg) {
				oldLogMsg = JSON.parse(oldLogMsg)
				$("#txt_phone").val(oldLogMsg.userName)
				
				localStorage.removeItem('oldLogMsg')
			}
		});

		//1.9.7
		function identityChange(type) {
			if (type == 0) {
				// console.log($('input[id="identityF1"]:checked').val())
				isIdLogin = $('input[id="identityF1"]:checked').val()
				$("#idCardInput").hide()
				$("#txtPhone").show()
				$("#SMS").show()
			} else {
				// console.log($('input[id="identityF2"]:checked').val())
				isIdLogin = $('input[id="identityF2"]:checked').val()
				$("#idCardInput").show()
				$("#txtPhone").hide()
				$("#SMS").hide()
			}

			$("#txt_paw").val("")
			strPassWord = "";
		}

		function check() {
			strUserName = $("#txt_phone").val().trim();
			var myreg = /^1[3-9]\d{9}$/;
			if (strUserName.length == 11 && myreg.test(strUserName)) {
				let params = {
					"mobile": strUserName,
				}
				newAjaxPost("/h5/login/judgeUser", params, checkOk);
			}
		}

		function checkId() {
			userIdCard = $("#txt_IDcard").val().trim();
		}

		function checkOk(jsonInfo) {
			if (jsonInfo.code == 1) {
				if (jsonInfo.data.list.length == 1 || (jsonInfo.data.list.length == 2 && jsonInfo.data.list[1] == 3)) {
					//单个角色
					roles = 1;
					// role = jsonInfo.data.list[0]
					// if (role == 1) { //用户
					// 	$("#SMS").removeClass("disappear")
					// } else if (role == 2) { //服务点
					// 	$("#SMS").addClass("disappear")
					// }
					userNotFound = false;
				} else if (jsonInfo.data.list.length >= 2) {
					// 用户和服务点都有
					roles = 2;
					userNotFound = false;
				} else {
					msgInfo("该账号尚未注册");
					userNotFound = true;
				}
			} else {
				msgInfo(jsonInfo.msg);
			}
		}

		function noClick() {
			if (countdown == 0) {
				$('#v_container').text('重新发送')
				$('#v_container').removeClass("notClickn")
				countdown = 60;
				return;
			} else {
				$('#v_container').text('重新发送(' + countdown + ')')
				$('#v_container').addClass("notClickn")
				countdown--;
			}
			setTimeout(function () {
				noClick()
			}, 1000)
		}

		function doReg() {
			/* goPage('inquiry_user_reg.htm'); */
			goPage("inquiry_user_reg.htm")
		}

		function doForget() {
			/* goPage('inquiry_user_reg.htm'); */
			goPage("inquiry_user_forget.htm")
		}
		$('#btnBack').on("click", function () {
			goPage("inquiry_user_personal.htm")
		});

		function get_captcha() {
			strUserName = $("#txt_phone").val().trim();
			var myreg = /^1[3-9]\d{9}$/;
			if (!myreg.test(strUserName)) {
				msgInfo("请输入正确的手机号码！");
				return false;
			} else if(userNotFound) {
				msgInfo("该账号尚未注册");
				return false;
			} else {
				let params = {
					"mobile": strUserName,
					"type": 1
				}
				newAjaxPost("/h5/captcha/phoneCaptcha", params, get_captchaOk);
				return true;
			}
		}

		function get_captchaOk(jsonInfo) {
			if (jsonInfo.code == 1) {
				noClick()
				msgInfo("已发送短信验证码,请注意查收！");
			} else {
				msgInfo(jsonInfo.msg);
			}
		}

		function select() {
			newDoLogin()
		}

		function disappear() {
			$('.weui-mask').removeClass('weui-mask--visible')
			$('.weui-dialog').removeClass('weui-dialog--visible')
		}

		function newDoLogin() {
			var myreg = /^1[3-9]\d{9}$/;
			var idReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
			strUserName = $("#txt_phone").val().trim();
			strPassWord = $("#txt_paw").val().trim();
			userIdCard = $("#txt_IDcard").val().trim();
			code = $("#code_input").val();

			if (userNotFound) {
				msgInfo('该账号尚未注册')
				return false
			}
			if ((!isHis && isSpace(strUserName)) || (isIdLogin == 1 && isHis && isSpace(strUserName))) {
				msgInfo("手机号码不能为空！");
				return false;
			}
			if (isIdLogin == 2 && isSpace(userIdCard)) {
				msgInfo("身份证号码不能为空！");
				return false;
			}
			if (isIdLogin == 2 && !idReg.test(userIdCard)) {
				msgInfo("请输入正确的身份证号码");
				return false;
			}
			if ((!isHis && !myreg.test(strUserName)) || (isIdLogin == 1 && isHis && !myreg.test(strUserName))) {
				msgInfo("请输入正确的手机号码！");
				return false;
			}
			if (isSpace(strPassWord)) {
				msgInfo("登录密码不能为空！");
				return false;
			}
			if(strPassWord.length < 6 || strPassWord.length > 20){
				msgInfo('请输入6-20位密码', 1200)
				return
			}
			if(!$('#agree').is(':checked')){
				msgInfo(`请先勾选《${agreementTitle}》`)
				return
			}
			if (role == 3) {
				msgInfo("医生请前往小程序登录！");
				return false;
			}

			//请求看后端怎么改

			// documentNumber	
			var paramJson = {
				// "mobile": strUserName,
				"password": strPassWord,
				// "smsCode": code
				userType: 'P'
			};
			if (isIdLogin == 2) {
				paramJson.documentNumber = userIdCard
			}else{
				paramJson.mobile = strUserName
			}
			// if (role == 1) { //用户
			paramJson.smsCode = code
			if (isIdLogin == 1 && isSpace(code)) {
				msgInfo("短信验证码不能为空！");
				return false;
			}
			// } else if (role == 2) { //门店
			// 	//不需要code
			// }
			paramJson.code = wxcode;

			// 缓存账号和验证码 密码错误时重定向之后自动填入
			let oldUser = {
				userName: strUserName
			}
			localStorage.setItem('oldLogMsg',JSON.stringify(oldUser))
			
			newAjaxPost("/h5/login/login", paramJson, newDoLoginOk);
		}
		//登录成功
		function newDoLoginOk(jsonInfo) {
			// console.log(jsonInfo)
			if (jsonInfo.code == '1') {
				
				userJson = jsonInfo.data,
					success = 0;
				// console.log(userJson.list);
				if (userJson.list.length == 1) {
					if (userJson.list[0].type == 'P') { //用户
						msgInfo('登录成功');
						success++;
						localStorage.setItem('user_id', userJson.list[0].id);
						setUserInfo(userJson.list[0]);
						setUserType('1');
						goPage('inquiry_doctor_index.htm');
					} else if (userJson.list[0].type == 'S') { //门店
						msgInfo('登录成功');
						success++;
						localStorage.setItem('user_id', userJson.list[0].id);
						setUserInfo(userJson.list[0]);
						setUserType('2');
						goPage('inquiry_doctor_index.htm');
					} else { //医生不操作
						msgInfo('医生端请到小程序登录');
						setTimeout(getNewAuth, 1000)
					}
				} else if (userJson.list.length > 1) {
					let jsonList = []
					userJson.list.forEach((item, index) => {
						if (item.type != "D") {
							jsonList.push(item)
						}
					})

					if (jsonList.length == 1) {
						if (jsonList[0].type == 'P') { //用户
							msgInfo('登录成功');
							success++;
							localStorage.setItem('user_id', jsonList[0].id);
							setUserInfo(jsonList[0]);
							setUserType('1');
							goPage('inquiry_doctor_index.htm');
						} else { //门店
							msgInfo('登录成功');
							success++;
							localStorage.setItem('user_id', jsonList[0].id);
							setUserInfo(jsonList[0]);
							setUserType('2');
							goPage('inquiry_doctor_index.htm');
						}
					} else {
						$('.weui-mask').addClass('weui-mask--visible')
						$('.weui-dialog').addClass('weui-dialog--visible')
					}
				}
			} else{
				// 密码错误 重新跳转到微信oauth2授权获取新的code
				setTimeout(getNewAuth, 1000)
				msgInfo(jsonInfo.msg);
			}
		}


		function Gologin() {
			role = $("input[name='f1']:checked").val();
			// console.log(role, userJson);
			userJson.list.forEach((item, index) => {
				if (role == 1 && item.type == 'P') {
					msgInfo('登录成功');
					success++;
					localStorage.setItem('user_id', userJson.list[index].id);
					setUserInfo(userJson.list[index]);
					setUserType('1');
					goPage('inquiry_doctor_index.htm');
				} else if (role == 2 && item.type == 'S') { //门店
					msgInfo('登录成功');
					success++;
					localStorage.setItem('user_id', userJson.list[index].id);
					setUserInfo(userJson.list[index]);
					setUserType('2');
					goPage('inquiry_doctor_index.htm');
				}
			})
		}

		// 获取新授权code
		function getNewAuth() {
			let {origin, pathname} = window.location,
				local = origin + pathname

			var APPID = getAPPID();
			window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID +
					'&redirect_uri=' + encodeURIComponent(local) +
					'&response_type=code&scope=snsapi_base&state=#wechat_redirect'
		}

		// 强制关注公众号，获取openid
		function getCode() {
			var code = getUrlParam('code') // 截取路径中的code，如果没有就去微信授权，如果已经获取到了就直接传code给后台获取openId
			
			if (code == null || code === '') {
				getNewAuth()
			} else {
				getOpenId(code) //把code传给后台获取openid
			}
		}
		//把code传给后台,得到openid
		function getOpenId(code) {
			wxcode = code
			// sessionStorage.setItem("code", code);
		}
		//获取地址栏的参数
		function getUrlParam(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			if (r != null) return unescape(r[2]);
			return null;
		}
		

		getAgreement()
		$('#agreementTitle').on('click', function (e) {
			// if($(this).is(':checked')){
				savePassword()
				goPage('user_agreement.htm')
			// }
		})

		function getAgreement(){
			newAjaxPost('/h5/utilInitData/getAgreement', {id: 2}, function (res) {
				agreementTitle = res.data.initName
				$('#agreementTitle').text('《' + agreementTitle + '》')
			})
		}

		function savePassword(){
			let pw = $("#txt_paw").val()
			if(pw){
				sessionStorage.setItem('loginPw', pw)
			}
		}

		window.onpageshow = function (){
			// if($('#agree').is(':checked')){
				$("#txt_paw").val(sessionStorage.getItem('loginPw') || '')
				sessionStorage.removeItem('loginPw')
			// }
		}

		// 显示密码==================
		const hidePsw = ''
		const showPsw = '../res/images/1.9.1img/eye_icon.png'
		let showingPsw = false

		$(".togglePsw").prop('src', hidePsw).show().on('click', function(){
			showingPsw = !showingPsw

			showingPsw ? $(this).prop('src', showPsw).addClass('eye-open').prev().prop('type', 'text')
				: $(this).prop('src', hidePsw).removeClass('eye-open').prev().prop('type', 'password')
		})

	</script>
</body>

</html>